// 2013-8-3下午06:16:45  net.betou.trade.front.action.NoticeAct.java

package net.betou.trade.front.action;

import static net.betou.common.page.SimplePage.cpn;
import static net.betou.common.page.SimplePage.cpnPageSize;
import static net.betou.web.Constants.NOTICE;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.page.Pagination;
import net.betou.common.security.encoder.PwdEncoder;
import net.betou.common.web.Constants;
import net.betou.common.web.CookieUtils;
import net.betou.common.web.RequestUtils;
import net.betou.trade.entity.main.Notice;
import net.betou.trade.entity.main.User;
import net.betou.trade.main.manager.NoticeMng;
import net.betou.trade.main.manager.UserMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.WebErrors;
import net.betou.web.FrontUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author 叶装装
 */
@Controller
public class NoticeAct{
	public static final String NOTICE_TPL = "公告预览";
	public static final String NOTICE_LIST_TPL = "系统公告页面";
	
	/**
	 * 看了公告之后，就要去除客户端的cookie
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping( "/notice/view.jhtml" )
	public String toView( Integer id, HttpServletRequest request, HttpServletResponse response, ModelMap model ) {
		
		FrontUtils.frontData( request, model );
		
		WebErrors errors = WebErrors.create( );

		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return FrontUtils.showMessage( request, model, errors.getErrors( ) );
		}

		Notice notice = noticeMng.findById( id );

		model.addAttribute( "notice", notice );

		CookieUtils.addCookie( request, response, Constants.POP_NOTICE_KEY, notice.getId( ).toString( ), Integer.MAX_VALUE, null );

		

		return FrontUtils.getTplPath( NOTICE, NOTICE_TPL );
	}
	
	@SuppressWarnings( "unchecked" )
	@RequestMapping( "/notice/list.jhtml" )
	public String toList( String username, String password, Integer pageSize, Integer pageNo, HttpServletRequest request, HttpServletResponse response, ModelMap model  ){
		FrontUtils.frontData( request, model );
		
		username = RequestUtils.parseParamUsingUTF8( username );
		
		WebErrors errors = validateList( username, password );
		// 返回错误消息
		if( errors.hasErrors( ) ){
			return FrontUtils.showMessage( request, model, errors.getErrors( ) );
		}
		
		FrontUtils.frontData( request, model );
		
		User user = userMng.findByUsername( username );
		
		pageSize = cpnPageSize( pageSize );
		pageNo = cpn( pageNo );
		
		Pagination pagination = noticeMng.getPage(  user, pageNo, pageSize );
		
		List list = pagination.getList( );
		
		model.addAttribute( "pagination", pagination );
		
		if( list.size( ) > 0 ){
			model.addAttribute( "notice", list.get( 0 ) );
		}
		
		
		model.addAttribute( "hrefFormer", "/notice/list.jhtml?username=" + username + "&password=" + password );
		
		return FrontUtils.getTplPath( NOTICE, NOTICE_LIST_TPL );
	}
	
	private WebErrors validateList( String username, String password ) {

		WebErrors errors = WebErrors.create( );

		if( StringUtils.isBlank( username ) || StringUtils.isBlank( password ) ){
			errors.addError( Messages._403 );
			return errors;
		}

		User user = userMng.findByUsername( username );

		if( user == null ){
			errors.addError( Messages._403 );
			return errors;
		}

		// 密码不正确
		if( !pwdEncoder.isPasswordValid( user.getPassword( ), password, user.getSalt( ) ) ){
			errors.addError( Messages._403 );
			return errors;
		}
		
		return errors;

	}

	@Autowired
	private UserMng userMng;

	@Autowired
	private PwdEncoder pwdEncoder;

	@Autowired
	private NoticeMng noticeMng;

}
